define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
],function($,IO,bootbox,bootstrap,upui,up,jui){
    var size;
    $("#fileupload").on("change",function(){
        var file2 = document.getElementById("fileupload");
        var fileSize = file2.files[0];
        size = Math.round(fileSize.size/1024);
    })
    /**-------------数据展示----------------*/
    $(function () {
        var grid;
        //每列数据列性
        var columns = [
            {
                title: '资料类型',
                dataIndex: 'formatter',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].formatter+"</span>";
                }
            },
            {
                title: '资料名称',
                dataIndex: 'asset_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].asset_name +"</span>";
                }
            },
            {
                title: '上传时间',
                dataIndex: 'timestamp',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var date = new Date(data[datarow].timestamp);
                    create_year = date.getFullYear();
                    create_month = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
                    create_day = date.getDate() < 10 ? '0'+date.getDate() : date.getDate() ;
                    return "<span>"+create_year+'-'+create_month+'-'+create_day+"</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span><a href='javascript:void(0)' style='color: red;' class='delete' asset_id='"+data[datarow].asset_id+"'>删除</a></span>";
                }
            },

        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/ct/act/databaseManagement", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {
            var asset_name = $("#searchContent").val();
            var param = "asset_name="+asset_name;
            return param;
        }
        dataInit();
        //点击查询
        $("#searchid").bind("click", function () {
            dataInit();
        });

    });

    /**--------------------------------删除操作-------------------------------*/
    $(document).on("click",".delete",function(){
        var asset_id=$(this).attr("asset_id");
        if(asset_id){
            bootbox.confirm("确定要删除该资料？",function(result){
                if(result) {
                    IO.get("/ct/act/data_delete",{asset_id:asset_id},function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/databaseManagement';
                            }
                        })
                    });
                }
            });
        }
    });
    /**--------------------------------文件上传-------------------------------*/
    $('#fileupload').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        //acceptFileTypes: /(\.|\/)(xls|xls?x)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        $("#cover").show();
    }).on('fileuploadprocessalways', function (e, data) {

    }).on('fileuploadprogressall', function (e, data) {

    }).on('fileuploaddone', function (e, data) {
        if(data.result.code==0){
            var fileUrl = data.result.url;//获取url
            var fileTotalName = data.result.filename;//获取文件名
            var arr = fileTotalName.split(".");
            var fileName = arr[0];
            var fileType = arr[1];

            var params = {asset_name:fileName,asset_url:fileUrl,formatter:fileType,size:size}
            IO.post('/ct/act/database_upload',params,function(d){
                bootbox.alert(d.msg,function(){
                    location.href = '/databaseManagement';
                });
            })
        }else{
            bootbox.alert('上传失败',function(){
                location.href = '/databaseManagement';
            });
        }

    });
})
